package com.info.zhiduoduo.core.service.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
//import com.info.zhiduoduo.common.configs.DianDianYunConfig;
import com.info.zhiduoduo.common.configs.DianDianYunConfig;
import com.info.zhiduoduo.common.utils.FileUtil;
import com.info.zhiduoduo.common.utils.HttpUtil;
import com.info.zhiduoduo.common.utils.LogUtil;
import com.info.zhiduoduo.common.utils.StringUtil;
import com.info.zhiduoduo.core.formbean.diandianyun.ImgIdCardFormBean;
import com.info.zhiduoduo.core.formbean.diandianyun.RealNameFormBean;
import com.info.zhiduoduo.core.formbean.diandianyun.common.CommonBaseSimpleFormBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class DianDianYunService {

    @Resource
    DianDianYunConfig dianDianYunConfig;

    @Resource
    RedisService redisService;

    @Resource
    RestTemplate restTemplate;

    public Map<String, Object> idCardAnalysis(String path, String idCardSide){
        Map<String, Object> map = new HashMap<>();
        //将图片转为byte[]
        byte[] bytes = null;
        try {
            File file = new File(path);
            bytes = FileUtil.fileToBytes(file);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state","error");
            map.put("msg","点点云身份证解析-图片转byte[]失败！");
            return map;
        }
        //获取token
        String token = redisService.getString("diandianyun");
        if(StringUtil.isEmpty(token)){
            map.put("state","error");
            map.put("msg","点点云身份证解析-获取token失败！");
            return map;
        }
        //构建请求参数
        ImgIdCardFormBean imgIdCardFormBean = new ImgIdCardFormBean().setBytes(bytes).setIdCardSide(idCardSide);
        CommonBaseSimpleFormBean<ImgIdCardFormBean> formBean = new CommonBaseSimpleFormBean<>();
        formBean.setReqdata(imgIdCardFormBean);
        formBean.setAppId(dianDianYunConfig.getDianDianYunAppId());
        formBean.setAuthToken(token);
        JSONObject jsonObject = restTemplate.postForObject(dianDianYunConfig.getUrl() + dianDianYunConfig.getImgIdCard(), formBean,
                JSONObject.class);

//        Map<String, String> params = new HashMap<>();
//        params.put("reqdata", JSON.toJSONString(imgIdCardFormBean));
//        params.put("appId",dianDianYunConfig.getDianDianYunAppId());
//        params.put("authToken",token);
//        String result = HttpUtil.sendPost(dianDianYunConfig.getUrl() + dianDianYunConfig.getImgIdCard(),params);
//
//        LogUtil.info("=================" + result);
//        JSONObject jsonObject = JSONObject.parseObject(result);

        if(null != jsonObject && "100".equals(jsonObject.getString("state"))){
            JSONObject object = jsonObject.getJSONObject("object");
            if(null != object){
                map.put("state","success");
                map.put("object",object);
                return map;
            } else {
                map.put("state","error");
                map.put("msg","点点云身份证解析-object返回为空！");
                return map;
            }
        } else {
            if(null == jsonObject){
                map.put("state","error");
                map.put("msg","点点云身份证解析-返回为空！");
                return map;
            } else {
                map.put("state","error");
                map.put("msg","点点云身份证解析-失败原因：" + jsonObject.getString("msg") + "！");
                return map;
            }
        }
    }

    public Map<String, Object> realName(String name, String idCardNumber){
        Map<String, Object> map = new HashMap<>();
        //获取token
        String token = redisService.getString("diandianyun");
        if(StringUtil.isEmpty(token)){
            map.put("state","error");
            map.put("msg","点点云实名认证-获取token失败！");
            return map;
        }
        //构建请求参数
        RealNameFormBean realNameFormBean = new RealNameFormBean().setName(name).setIdCardNumber(idCardNumber);
        CommonBaseSimpleFormBean<RealNameFormBean> formBean = new CommonBaseSimpleFormBean<>();
        formBean.setReqdata(realNameFormBean);
        formBean.setAppId(dianDianYunConfig.getDianDianYunAppId());
        formBean.setAuthToken(token);
        JSONObject jsonObject = restTemplate.postForObject(dianDianYunConfig.getUrl() + dianDianYunConfig.getCheckIdMatch(), formBean,
                JSONObject.class);
        if(null != jsonObject && "100".equals(jsonObject.getString("state"))){
            map.put("state","success");
            return map;
        } else {
            if(null == jsonObject){
                map.put("state","error");
                map.put("msg","点点云实名认证-返回为空！");
                return map;
            } else {
                map.put("state","error");
                map.put("msg","点点云实名认证-失败原因：" + jsonObject.getString("msg") + "！");
                return map;
            }
        }
    }

    /**********************************************获取点点云Token并放入Redis**********************************************/
    public void getToken(){
        JSONObject jsonObject = restTemplate.getForObject(dianDianYunConfig.getUrl() + dianDianYunConfig.getGetToken()
                        + "?appId=" + dianDianYunConfig.getDianDianYunAppId() +"&appSecret=" + dianDianYunConfig.getDianDianYunApiSecret(),
                        JSONObject.class);
        if(null != jsonObject && "100".equals(jsonObject.getString("state"))){
            JSONObject object = jsonObject.getJSONObject("object");
            if(null != object && StringUtil.isNotEmpty(object.getString("authToken"))){
                boolean b = redisService.set("diandianyun",object.getString("authToken"));
                if(!b){
                    LogUtil.error("点点云获取Token失败！放入缓存失败！");
                }
            } else {
                LogUtil.error("点点云获取Token失败！object为空或authToken为空！返回：" + object);
            }
        } else {
            if(null == jsonObject){
                LogUtil.error("点点云获取Token失败！返回为空");
            } else {
                LogUtil.error("点点云获取Token失败！返回：" + jsonObject.toJSONString());
            }
        }
    }
}
